import Foundation
import Flutter
import UIKit
import HelpDesk
import Hyphenate

public class HxDemoPlugin: NSObject, FlutterPlugin {
    
    
  public static func register(with registrar: FlutterPluginRegistrar) {
      
    let channel = FlutterMethodChannel(name: "hx_demo", binaryMessenger: registrar.messenger())
    let instance = HxDemoPlugin()
    registrar.addMethodCallDelegate(instance, channel: channel)
  }

    public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
        //    result("iOS " + UIDevice.current.systemVersion)
        
        if (call.method == "init"){
            initMethod(call: call, result: result)
        }else if (call.method == "register"){
            registerMethod(call: call, result: result)
        }else if (call.method == "login"){
            loginMethod(call: call, result: result)
        }else if (call.method == "isLogin"){
            isLoginMethod(call: call, result: result)
        }else if (call.method == "logout"){
            logoutMethod(call: call, result: result)
        }
        
    }
    
    public func initMethod(call: FlutterMethodCall, result: @escaping FlutterResult){
        
        let option = HDOptions()
        let arguments = call.arguments  as! [String:Any]
        option.appkey = arguments["appKey"] as? String
        option.tenantId = arguments["tenantId"] as? String
        option.enableDnsConfig
//        imOptions.enableFpa = YES;// 设置对应的im参数
//        imOptions.usingHttpsOnly = NO; //设置对应的im参数
        print(arguments)
        HDClient.shared().initializeSDK(with: option)
        result("成功");
    }
    
    public func registerMethod(call: FlutterMethodCall, result: @escaping FlutterResult){
        let arguments = call.arguments  as! [String:Any]
        let username = arguments["username"] as? String
        let password = arguments["password"] as? String
        var error = HDError()
        error = HDClient.shared().register(withUsername: username, password: password)
        if(error.code == HDErrorUserAlreadyExist){
            result(NSNumber(value: true))
        }else{
            result(NSNumber(value: false))
        }
   
    }
    
    public func loginMethod(call: FlutterMethodCall, result: @escaping FlutterResult){
        let arguments = call.arguments  as! [String:Any]
        let username = arguments["username"] as? String
        let password = arguments["password"] as? String
        var error = HDError()
        error = HDClient.shared().login(withUsername: username, password: password)
        if(error.code == HDErrorUserAlreadyExist){
            result(NSNumber(value: true))
        }else{
            result(NSNumber(value: false))
        }
    }
    
    public func isLoginMethod(call: FlutterMethodCall, result: @escaping FlutterResult){
        
        if(HDClient.shared().isLoggedInBefore){
            result(NSNumber(value: true))
        }else{
            result(NSNumber(value: true))
        }
    }
    
    public func logoutMethod(call: FlutterMethodCall, result: @escaping FlutterResult){
        
        let error = HDClient.shared().logout(true)
        if error != nil {
            result(NSNumber(value: true))
        }else{
            result(NSNumber(value: true))
        }
        
    }
    
}


